function levelnode(root){
    if(!root) return null;
    let queue = [root];
    const res = [];
    while(queue.length > 0){
        const level = []
        for(let i = queue.length; i > 0; i--){
            const node = queue.shift();
            level.push(node.val);
            if(node.left) queue.push(node.left);
            if(node.right) queue.push(node.right);
        }
        res.push(...level);
    }
    return res;
}

// 测试用例1: 空树
console.log("测试用例1 - 空树:");
console.log(levelnode(null)); // 预期输出: null

// 测试用例2: 只有根节点
console.log("测试用例2 - 只有根节点:");
const root1 = { val: 1, left: null, right: null };
console.log(levelnode(root1)); // 预期输出: []

// 测试用例3: 完全二叉树
console.log("测试用例3 - 完全二叉树:");
const root2 = {
  val: 3,
  left: {
    val: 9,
    left: null,
    right: null
  },
  right: {
    val: 20,
    left: {
      val: 15,
      left: null,
      right: null
    },
    right: {
      val: 7,
      left: null,
      right: null
    }
  }
};
console.log(levelnode(root2)); // 预期输出: []

// 测试用例4: 不平衡树
console.log("测试用例4 - 不平衡树:");
const root3 = {
  val: 1,
  left: {
    val: 2,
    left: {
      val: 3,
      left: {
        val: 4,
        left: null,
        right: null
      },
      right: null
    },
    right: null
  },
  right: {
    val: 5,
    left: null,
    right: null
  }
};
console.log(levelnode(root3)); // 预期输出: []

